package com.fenxiangle.yueding.feature.mine.view;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowInsets;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dueeeke.videoplayer.player.IjkPlayer;
import com.dueeeke.videoplayer.player.IjkVideoView;
import com.dueeeke.videoplayer.player.PlayerConfig;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.util.StandardVideoController;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class VideoPlayerActivity extends BaseActivity {

    @BindView(R.id.layout_video)
    LinearLayout layoutVideo;
    @BindView(R.id.video_dec)
    TextView videoDec;
    @BindView(R.id.video_pler_creditScore)
    TextView videoPlerCreditScore;
    @BindView(R.id.video_pler_timeValue)
    TextView videoPlerTimeValue;
    @BindView(R.id.video_pler_fans)
    TextView videoPlerFans;
    @BindView(R.id.video_back)
    ImageView videoBack;
    @BindView(R.id.video_stop)
    ImageView videoStop;
    @BindView(R.id.layout_video_rr)
    RelativeLayout layoutVideoRr;
    @BindView(R.id.video_player)
    IjkVideoView videoPlayer;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_video_player;
    }

    @Override
    protected void initInjector() {

    }

    private String videoCoverImg, videoPath, dec;
    UserCenterBo userCenterBo;
    private StandardVideoController controller;
    private PlayerConfig mPlayerConfig = null;

    @Override
    protected void initData() {
        videoCoverImg = getIntent().getStringExtra("videoCoverImg");
        videoPath = getIntent().getStringExtra("videoPath");
        dec = getIntent().getStringExtra("dec");
        userCenterBo = (UserCenterBo) getIntent().getSerializableExtra("userCenterBo");
        setStatusBarTransparent();


        videoDec.setText(TextUtils.isEmpty(dec) ? "" : dec);
        if (userCenterBo != null) {
            videoPlerCreditScore.setText("信用分\n" +  userCenterBo.getCreditScore());
            videoPlerTimeValue.setText("时间值\n" +  "￥" + userCenterBo.getTimeValue());
            videoPlerFans.setText("粉丝\n" + userCenterBo.getFans() + "人");
        }
        controller = new StandardVideoController(this);
        mPlayerConfig = new PlayerConfig.Builder()
//                    .enableCache()
//                    .autoRotate()
                .setLooping()
                .setCustomMediaPlayer(new IjkPlayer(VideoPlayerActivity.this) {
                    @Override
                    public void setOptions() {
                        //精准seek
                        mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1);
                    }
                })
                .addToPlayerManager()
                .savingProgress()
                .build();
        videoPlayer.setPlayerConfig(mPlayerConfig);
        videoPlayer.setUrl(videoPath);
        AM.image().bind(videoCoverImg, controller.getThumb());
        videoPlayer.setVideoController(controller);
        videoPlayer.start();
        videoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP;

        return super.onTouchEvent(event);
    }

    /**
     * 把状态栏设成透明
     */
    private void setStatusBarTransparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            View decorView = VideoPlayerActivity.this.getWindow().getDecorView();
            decorView.setOnApplyWindowInsetsListener((v, insets) -> {
                WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                return defaultInsets.replaceSystemWindowInsets(
                        defaultInsets.getSystemWindowInsetLeft(),
                        0,
                        defaultInsets.getSystemWindowInsetRight(),
                        defaultInsets.getSystemWindowInsetBottom());
            });
            ViewCompat.requestApplyInsets(decorView);
            getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoPlayer.release();
    }


    @Override
    public void onBackPressed() {
        if (!videoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
